import { observable, action ,runInAction,toJS} from "mobx";


// 编辑优惠券
export default class CouponEdit{
  

  
  // 优惠ID
  @observable id=0;

  @observable loading=false;

  // 数据
  @observable data={};
  // 副本
  @observable dataDouble={};
  // 时效
  @observable tm=-1;
  // 相对时间
  @observable tmRange="";
  // 时间后缀
  @observable sfx=1;
  // 领用次数
  @observable num=0;
  // 折扣条件
  @observable conditions=-1;
  // 满减   满conditionsAfter减conditionsBefore
  @observable conditionsAfter="";
  @observable conditionsBefore="";
  

  
  constructor(root){
    this.root = root;
  }

  @action set=(k,v)=>{
    const arr = k.split("-");
    const loop = (obj,arr,v)=>{
      const temp = arr.splice(0,1);
      if(arr.length){
        if(!obj[temp]){
          obj[temp]={};
        }
        obj = obj[temp];
        loop(obj,arr,v);
      }else{
        try{
          obj[temp] = v;
        }catch(e){
          console.log("error:obj[temp]",`obj[${temp}]`);
        }
        
      }
    };
    loop(this,arr,v);
  };
  
  // 初始化
  @action init=()=>this._init();
  _init(){
    this.set("loading",false);
    this.set("sfx",1);
    this.set("tm",-1);
    // 相对时间的值
    this.set("tmRange","");
    this.set("conditions",-1);
    this.set("conditionsAfter","");
    this.set("conditionsBefore","");

    this.set("num",0);
    this.set("data",{scope:0,sys:0});
    if(this.id){this._select();}
  }

  _select(){
    this.set("loading",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{
        act:"admin-coupon-select",
        id:this.id
      },
      success:json=>{
        console.log(json);
        this.set("loading",false);
        if(json.status !== "ok"){this.root.alert(json);return;}
        this.set("data",json.data);
        this.set("dataDouble",json.data);
        this.set("tm",/^\d+/.test(json.data.tm)?0:1);
        this.set("num",json.data.num?1:0);
        this.set(
          "conditions",
          /^-\d+/.test(json.data.conditions)?
          0:
          (/^\d+-\d+/.test(json.data.conditions)?1:2)
        );

        // 使用时效
        const tm = /^\d+-\d+$/.test(json.data.tm)?0:1;
        this.set("tm",tm);
        if(tm){
          if(json.data.tm>=2592000){
            this.set("sfx",2592000);
            this.set("tmRange",json.data.tm/2592000);
          }else if(json.data.tm>=86400){
            this.set("sfx",86400);
            this.set("tmRange",json.data.tm/86400);
          }else if(json.data.tm>=3600){
            this.set("sfx",3600);
            this.set("tmRange",json.data.tm/3600);
          }else if(json.data.tm>=60){
            this.set("sfx",60);
            this.set("tmRange",json.data.tm/60);
          }else{
            this.set("sfx",1);
            this.set("tmRange",json.data.tm);
          }
        }
      }
    });
  }
  
  // 保存优惠券配置
  @action submit(){
    if(!this.data.tm){
      this.root.antd.modal.error({title:"错误",content:"请输入时效"});
    }else if(!this.data.conditions){
      this.root.antd.modal.error({title:"错误",content:"请输入使用条件"});
    }else if(!this.data.starttime){
      this.root.antd.modal.error({title:"错误",content:"请输入开始时间"});
    }else if(!this.data.endtime){
      this.root.antd.modal.error({title:"错误",content:"请输入结束时间"});
    }else{

      // console.log(this._parse());return;
      const data=this._parse();
      this.set("loading",true);
      this.root.ajax.post({
        url:this.root.url,
        data:data,
        success:json=>{
          this.set("loading",false);
          this.root.alert(json);
          if(json.status !== "ok"){return;}
          if(!this.id){
            this._init();
          }else{
            for(let k in this.data){
              if(this.data[k] !== this.dataDouble[k]){
                this.set(`dataDouble-${k}`,data[k]);
              }
            }
          }
        }
      })
    }
  }
  
  // 对比副本返回需要上传的数据
  _parse(){
    const data={};
    for(let k in this.data){
      if(this.data[k] !== this.dataDouble[k]){
        data[k] = this.data[k];
      }
    }
    
    if(this.id){
      data.data=JSON.stringify(data);
      data.id=this.id;
      data.act="admin-coupon-update";
    }else{
      data.data=JSON.stringify(this.data);
      data.act="admin-coupon-insert";
    }
    return data;

  }

}